package com.ruoyi.psi.purchase.apply.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApply;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApplyList;
import com.ruoyi.psi.purchase.apply.service.IPurchaseApplyService;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购申请单
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@RestController
@RequestMapping("/psi/purchase/apply")
@Api(value = "采购申请单信息模块", tags = "采购申请单接口")
public class PurchaseApplyController extends BaseController {


    @Autowired
    private IPurchaseApplyService purchaseApplyService;

    /**
     * 获取采购申请单
     * @param purchaseApply
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchaseApply.purchaseApplyId", "PurchaseApply.staffId", "PurchaseApply.departmentId", "PurchaseApply.applyTime"})
    @ApiOperation(value = "获取采购申请单列表",notes = "查询所有采购申请单信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PurchaseApply purchaseApply) {
        startPage();
        List<PurchaseApply> purchaseApplies = purchaseApplyService.selectPurchaseApplyList(purchaseApply);
        return getDataTable(purchaseApplies);
    }

    /**
     * 导出采购申请单
     *
     * @param purchaseApply
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PurchaseApply purchaseApply) {
        List<PurchaseApply> purchaseApplies = purchaseApplyService.selectPurchaseApplyList(purchaseApply);
        ExcelUtil<PurchaseApply> util = new ExcelUtil<PurchaseApply>(PurchaseApply.class);
        return util.exportExcel(purchaseApplies, "采购申请单数据");
    }

    /**
     * 新增采购申请单
     * @param purchaseApply 采购申请单单据
     * @return
     */
    @ApiOperation(value = "新增采购申请单", notes = "新增采购申请单")
    @ApiImplicitParam(name = "purchaseApply", value = "新增采购申请单", dataType = "PurchaseApply")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PurchaseApply purchaseApply) {

        try {
            purchaseApplyService.insertPurchaseApply(purchaseApply);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新采购申请单
     * @param purchaseApply 采购申请单单据
     * @return
     */
    @ApiOperation(value = "更新采购申请单", notes = "更新采购申请单")
    @ApiImplicitParam(name = "purchaseApply", value = "更新采购申请单", dataType = "PurchaseApply")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody PurchaseApply purchaseApply) {
        try {
            purchaseApplyService.updatePurchaseApply(purchaseApply);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除采购申请单
     * 仅修改删除标志位
     *
     * @param purchaseApply 采购申请单单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchaseApply purchaseApply) {

        return toAjax(purchaseApplyService.updatePurchaseApplyDeleteFlag(purchaseApply));
    }

    /**
     * 获取采购申请单详情
     * 采购跟单
     * @param purchaseApplyList
     * @return
     */
    @PostMapping("/selectPurchaseApplyList")
    public TableDataInfo selectPurchaseApplyList(@RequestBody PurchaseApplyList purchaseApplyList) {
        startPage();
        List<PurchaseApplyList> purchaseApplyLists = purchaseApplyService.selectPurchaseApplyListList(purchaseApplyList);
        return getDataTable(purchaseApplyLists);
    }


}
